package com.yonyou.crm.sys.dynamic.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.sys.dataright.entity.RightActionEnum;
import com.yonyou.crm.sys.dataright.entity.RightConfig;
import com.yonyou.crm.sys.dataright.entity.RightObjectTypeEnum;
import com.yonyou.crm.sys.dataright.entity.RightTypeEnum;
import com.yonyou.crm.sys.dataright.service.RightServiceImpl;
import com.yonyou.crm.sys.dynamic.entity.DynamicFollowTypeEnum;
import com.yonyou.crm.sys.dynamic.entity.DynamicFollowVO;
import com.yonyou.crm.sys.dynamic.entity.DynamicVO;
import com.yonyou.crm.sys.dynamic.filter.DateTimeFilter;
import com.yonyou.crm.sys.dynamic.filter.MyFilter;
import com.yonyou.crm.sys.user.entity.UserVO;
import com.yonyou.crm.sys.user.service.UserBizServiceImpl;

@Service
public class DynamicServiceImpl extends BaseBizServiceImpl<DynamicVO> {
	
	@Autowired
	RightServiceImpl rightService;
	@Autowired
	UserBizServiceImpl userService;
	
	public void insert(DynamicVO vo, Long[] followers) {
		UserVO user = getUser(vo.getOwnerId());
		// 插入动态本身
		vo = super.saveVO(vo);
		// 插入动态关注
//		if(followers != null && followers.length > 0){
//			List<DynamicFollowVO> follows = transFollow(user,vo,followers);
//			dao.saveSql(DynamicVO.class, "insertFollows", follows);
//		}
		// 插入动态关注
		List<DynamicFollowVO> follows = transFollow(user,vo,followers);
		dao.saveSql(DynamicVO.class, "insertFollows", follows);
	}

	private List<DynamicFollowVO> transFollow(UserVO user,DynamicVO vo, Long[] followers) {
		List<DynamicFollowVO> followVOs = new ArrayList<DynamicFollowVO>(); //负责人加1条
		//负责人
		DynamicFollowVO ownerVO = new DynamicFollowVO();
		ownerVO.setTenantId(vo.getTenantId());
		ownerVO.setCreatedTime(vo.getCreatedTime());
		ownerVO.setDeptId(user.getDeptId());
		ownerVO.setDynamicId(vo.getId());
		ownerVO.setFollowType(DynamicFollowTypeEnum.OWNER.getValue());
		ownerVO.setUserId(vo.getOwnerId());
		
		followVOs.add(ownerVO);
		
		//参与人
		if(followers != null && followers.length > 0){
			for(int i=0,len=followers.length;i<len;i++) {
				DynamicFollowVO followVO = new DynamicFollowVO();
				followVO.setTenantId(vo.getTenantId());
				followVO.setCreatedTime(vo.getCreatedTime());
				followVO.setDeptId(user.getDeptId());
				followVO.setDynamicId(vo.getId());
				followVO.setFollowType(DynamicFollowTypeEnum.FOLLOW.getValue());
				followVO.setUserId(followers[i]);
				
				followVOs.add(followVO);
			}
		}
		
		return followVOs;
	}
	public DynamicVO[] getDynamic(Long userId,Long roleId,int pageIndex,int pageSize) {
		UserVO user = getUser(userId);
		RightConfig config = rightService.getRightConfig(roleId, (long)RightObjectTypeEnum.DYNAMIC.getValue(), (long)RightActionEnum.VIEW.getValue());
		Long typeId = config.getTypeId();
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("pageIndex", String.valueOf(pageIndex));
		paramMap.put("pageSize", String.valueOf(pageSize));
		List<DynamicVO> vos = null;
		if (RightTypeEnum.OWNER.getValue() == typeId) {
			paramMap.put("userId", user.getId().toString());
			vos = dao.selectVoList(DynamicVO.class, "getDynamic_owner", paramMap);
		} else if (RightTypeEnum.SELFDEPT.getValue() == typeId) {
			// TODO
//			String deptPart = "";
			String deptPart = user.getDeptId().toString();
			paramMap.put("deptPart", deptPart);
			vos = dao.selectVoList(DynamicVO.class, "getDynamic_dept", paramMap);
		} else if (RightTypeEnum.SELFDEPTANDCHILD.getValue() == typeId) {
			// TODO
//			String deptPart = "";
			String deptPart = user.getDeptId().toString();
			paramMap.put("userId", user.getId().toString());
			paramMap.put("deptPart", deptPart);
			vos = dao.selectVoList(DynamicVO.class, "getDynamic_dept", paramMap);
		} else if (RightTypeEnum.SELFORG.getValue() == typeId) {
			// TODO
//			String orgPart = "";
			String orgPart = user.getOrgId().toString();
			paramMap.put("orgPart", orgPart);
			vos = dao.selectVoList(DynamicVO.class, "getDynamic_org", paramMap);
		} else if (RightTypeEnum.SELFORGANDCHILD.getValue() == typeId) {
			// TODO
			String orgPart = "";
			paramMap.put("orgPart", orgPart);
			vos = dao.selectVoList(DynamicVO.class, "getDynamic_org", paramMap);
		} else if (RightTypeEnum.TENANT.getValue() == typeId) {
			// TODO
		}
		if(vos == null) {
			return null;
		}
		//内置我Filter和时间格式Fitler
		MyFilter myFilter = new MyFilter(user.getId(),"我");
		DateTimeFilter dtFilter = new DateTimeFilter(System.currentTimeMillis());
		for(int i=0,len=vos.size();i<len;i++) {
			DynamicVO vo = vos.get(i);
			myFilter.doFilter(vo);
			dtFilter.doFilter(vo);
		}
		if(vos != null && vos.size() > 0){
			for(DynamicVO vo : vos){
				UserVO uvo = getUser(vo.getOwnerId());
				vo.setOwnerName(uvo.getName());
			}
		}
		return vos == null ? null : vos.toArray(new DynamicVO[0]);
	}

	/**
	 * 业务对象动态查询
	 * @param objType 对象类型
	 * @param userId 对象ID
	 * @return
	 */
	public DynamicVO[] getBusiDynamic(Long objType, Long objId) {
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("objType", objType.toString());
		paramMap.put("objId", objId.toString());
		List<DynamicVO> vos = dao.selectVoList(DynamicVO.class, "getBusiDynamic", paramMap);
		if(vos != null && vos.size() > 0){
			for(DynamicVO vo : vos){
				UserVO uvo = getUser(vo.getOwnerId());
				if (uvo != null) {
					vo.setOwnerName(uvo.getName());
				}
			}
		}
		return vos.toArray(new DynamicVO[0]);
	}

	/**
	 * 我的动态
	 * @param userId
	 * @return
	 */
	public DynamicVO[] getMyDynamic(Long userId) {
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("userId", userId.toString());
		List<DynamicVO> vos = dao.selectVoList(DynamicVO.class, "getMyDynamic", paramMap);
		//内置我Filter和时间格式Fitler
		MyFilter myFilter = new MyFilter(userId,"我");
		DateTimeFilter dtFilter = new DateTimeFilter(System.currentTimeMillis());
		for(int i=0,len=vos.size();i<len;i++) {
			DynamicVO vo = vos.get(i);
			myFilter.doFilter(vo);
			dtFilter.doFilter(vo);
		}
		return vos.toArray(new DynamicVO[0]);
	}
	
	private UserVO getUser(Long curUser) {
		UserVO user = userService.getDetail(curUser);
		return user;
	}
}
